#delimit;

use 20161021_Working_hh_1014.dta, clear;
drop if FIPS==11;


gen PSI_1=.5*19.790;
gen PSI_2=1*19.790;
gen PSI_3=1.5*19.790;
gen PSI_4=2*19.790;

gen GROSS=Y+totalT;

forvalues p=1/4{;
  
  gen POOR_`p'=1 if Y<=PSI_`p';
  replace POOR_`p'=0 if Y>PSI_`p' & Y~=.;
  gen POST_`p'=1 if GROSS<=PSI_`p';
  replace POST_`p'=0 if GROSS>PSI_`p' & GROSS~=.;
		
  };
  
sort state;  
collapse  POOR_* POST_* PSI_*, by(FIPS);

save 20161021_PROP.dta, replace;


use 20161021_Working_hh_1014.dta, clear;
keep if year>=2010;
drop if FIPS==11;


gen PSI_1=.5*19.790;
gen PSI_2=1*19.790;
gen PSI_3=1.5*19.790;
gen PSI_4=2*19.790;

gen GROSS=Y+totalT;


forvalues p=1/4{;

  
    gen POOR_`p'=1 if Y<=PSI_`p';
    replace POOR_`p'=0 if Y>PSI_`p' & Y~=.;
  	
  	gen GAP_`p'=(PSI_`p'-Y)/PSI_`p';
  	replace GAP_`p'=0 if GAP_`p'<0; 
  	gen POST_GAP_`p'=(PSI_`p'-GROSS)/PSI_`p';
  	replace POST_GAP_`p'=0 if POST_GAP_`p'<0; 

  };

sort state;  
collapse  POOR_* GAP* POST* PSI_*, by(FIPS);

save 20161021_GAP.dta, replace;



  
use   20161021_PROP.dta, clear;
merge 1:1 FIPS using 20161021_GAP.dta, nogen;
merge 1:1 FIPS using 20161021_State_Parameters_w_Var_Policy.dta, nogen;


forvalues p=1/4{;
	gen PROP_RED_`p'=(POOR_`p'-POST_`p')/POOR_`p';
	gen GAP_RED_`p'=(GAP_`p'-POST_GAP_`p')/GAP_`p';
	egen RANK_PROP_`p'=rank(PROP_RED_`p'), field;
	egen RANK_GAP_`p'=rank(GAP_RED_`p'), field;
	egen RANK_R_`p'=rank(R_`p'), field;

	};



	
twoway scatter GAP_RED_1 GAP_RED_2, mlab(state) m(i) mlabc(gs12) mlabsize(small)
	  || scatter GAP_RED_3 GAP_RED_2,  mlab(state) m(i) mlabc(gs8) mlabsize(small)
	  || scatter GAP_RED_4 GAP_RED_2,  mlab(state) m(i) mlabc(gs4) mlabsize(small)
	   legend(off)   graphregion(color(white)) ytitle("Proportional Reduction in Poverty Gap (Alternate Threshold)", size(small))
	   xtitle("Proportional Reduction in Poverty Gap", size(small)) ylabel(0(.1).6, nogrid labsize(small)) 
	   xlabel(.2(.1).6, nogrid labsize(small))
	   b1title("(Threshold: Poverty Line)", size(small));
   
graph save 20161021_Gap_Red.gph, replace;   

spearman GAP_RED*;
   

	

	

twoway scatter PROP_RED_1 PROP_RED_2, mlab(state) m(i) mlabc(gs12) mlabsize(small)
  	|| scatter PROP_RED_3 PROP_RED_2,  mlab(state) m(i) mlabc(gs8) mlabsize(small)
  	|| scatter PROP_RED_4 PROP_RED_2,  mlab(state) m(i) mlabc(gs4) mlabsize(small)
  	legend(off)   graphregion(color(white)) ytitle("Proportional Reduction in Poverty Rate (Alternate Threshold)", size(small))
   	xtitle("Proportional Reduction in Poverty Rate", size(small)) ylabel(0(.1).6, nogrid labsize(small)) 
   	xlabel(.1(.1).5, nogrid labsize(small))
   	text(.6 .1 "Alternate Thresholds, as % of Poverty Line", placement(east) size(vsmall))
   	text(.58 .12 "50%", color(gs12) placement(east) size(small)) 
   	text(.58 .21 "150%", color(gs8) placement(east) size(small)) 
   	text(.58 .31 "200%", color(gs4) placement(east) size(small))
   	b1title("(Threshold: Poverty Line)", size(small));
   
graph save 20161021_Prop_Red.gph, replace;   

spearman PROP*;
  
twoway scatter R_1 R_2, mlab(state) m(i) mlabc(gs12) mlabsize(small)
  || scatter R_3 R_2,  mlab(state) m(i) mlabc(gs8) mlabsize(small)
  || scatter R_4 R_2,  mlab(state) m(i) mlabc(gs4) mlabsize(small)
  legend(off)   graphregion(color(white)) ytitle("Poverty Relief Ratio (Alternate Threshold)", size(small))
  xtitle("Poverty Relief Ratio", size(small)) ylabel(0(.2)1.3, nogrid labsize(small)) 
  xlabel(.3(.1).7, nogrid labsize(small))
  b1title("(Threshold: Poverty Line)", size(small))
   ;
   
graph save 20161021_R_Rank_Order.gph, replace;   

spearman R_*;

graph combine 20161021_Prop_Red.gph 20161021_Gap_Red.gph 20161021_R_Rank_Order.gph, col(3);

graph export "20161021_Varying_Thresholds_Comparison.pdf", replace fontface(Helvetica);

replace MAX=MAX/1000;

forvalues i=1/4 {;
replace R_`i'=R_`i'*100;
regress R_`i' MAX_TANF;
xtile category_R_`i'=R_`i', nq(4); 

replace PROP_RED_`i'=PROP_RED_`i'*100;
regress PROP_RED_`i'  MAX_TANF;  
xtile category_PROP_`i'=PROP_RED_`i', nq(4);

replace GAP_RED_`i'=GAP_RED_`i'*100;
regress GAP_RED_`i'  MAX_TANF;  
xtile category_GAP_`i'=GAP_RED_`i', nq(4);


};
